<?php

//die('fichier desactive par securite (commenter cette ligne de code pour le reactiver)'); // LOCK

require('include/class/Player.class.php');
require('include/class/Record.class.php');

/* Fichiers de configuration */
//require('conf/paths.conf.php');		// Configuration des paths
require('include/conf/db.conf.php');		// Configuration de l'accès à la base de données

/* Librairies */
require('include/lib/database.lib.php');	// Classe de la base de données

$table = 'player_v1';

$db = Database::getInstance();

	$sql = "SELECT * FROM `$table`
	";
	$sth = $db->prepare($sql);
	//$sth->bindValue(':begin', $begin, PDO::PARAM_INT);
	$sth->execute();
	$i=0;

	if($result = $sth->fetchAll())
	{
		foreach($result as $line)
		{
			$cleaned_IP = explode(" ", trim($line['IP'])); // pour les IP avec un commentaire...
			$cleaned_IP = $cleaned_IP[0];
			
			$birthdate = new DateTime ("$line[Player_Birth]-01-01");
			$formattedBirthdate = $birthdate->format('Y-m-d');
			
			$register_date = new DateTime ('2011-01-01');
			$formattedRegister_date = $register_date->format('Y-m-d');
			
			// country
			$sql = "SELECT id FROM `country`
					WHERE `name`= '$line[Player_Country]'/* COLLATE SQL_Latin1_General_CP1_CI_AS*/
			";
			$sth = $db->prepare($sql);
			$sth->execute();
			$country = 116; // Kazakhstan (2)
			echo ++$i."\t| ".$line['Player_ID']." \t".$line['Player_Name'];
			if($result = $sth->fetch())
			{
				$country = $result['id'];
				echo " \n<br />";
			}
			else
			{
				echo "\t NO COUNTRY \n<br />";
			}
			
			$player = new Player(Array(
				'pid' 				=> $line['Player_ID'],
				'nickname' 			=> $line['Player_Name'],
				'password' 			=> $line['Player_Password'], // automatically crypted
				'email' 			=> $line['Player_Mail'],
				'displayed_email'	=> $line['Player_Mail'],
				'ip' 				=> $cleaned_IP, 
				'birthdate'	 		=> $formattedBirthdate,
				'register_date' 	=> $formattedRegister_date,
				'country_id' 		=> $country,
				'description' 		=> "",
				'admin_level' 		=> 0,
				'state' 		=> 0
			));
			
			$player->save();
		}
	}
	echo 'Done!';


?>